export function formatDate(val, timeFlag) {
    let date = new Date(val);
    var y = date.getFullYear()
    var m = date.getMonth() + 1
    m = m < 10 ? '0' + m : m
    var d = date.getDate()
    d = d < 10 ? '0' + d : d
    var hour = date.getHours()
    hour = hour < 10 ? '0' + hour : hour
    var minute = date.getMinutes()
    minute = minute < 10 ? '0' + minute : minute
    var seconds = date.getSeconds()
    seconds = seconds < 10 ? '0' + seconds : seconds
    if (timeFlag) {
        return y + '-' + m + '-' + d + ' ' + hour + ':' + minute + ':' + seconds;
    } else {
        return y + '-' + m + '-' + d;
    }
}

export function getMonthAllDays(month) {
    let mon = '';
    let date = new Date();
    if (month) {
        mon = month
    } else {
        mon = new Date().getMonth() + 1;
    }
    let year = date.getFullYear();
    let monts = getThisMonth()
    let day = date.getDate();
    let weeks = ['日', '一', '二', '三', '四', '五', '六']
    let tempThisDay = formatDate(new Date(year + '-' + monts + '-' + day));
    let lastDay = getMonthLastDay();
    let d = '';
    let week = ''
    let dws = []
    let tempDay = '';
    let tempWeek = '',
        tempIsWeek = false
    for (let i = 0; i < lastDay; i++) {
        tempDay = (i + 1)
        tempWeek = weeks[new Date(year + '-' + monts + '-' + tempDay).getDay()]
        if (tempWeek === '六' || tempWeek === '日') {
            tempIsWeek = true
        } else {
            tempIsWeek = false
        }
        dws.push({
            day: tempDay,
            week: tempWeek,
            isWeek: tempIsWeek
        });
    }
    console.log(dws);
}


export function getMonthLastDay() {
    let date = new Date(),
        currentMonth = date.getMonth(),
        nextMonth = ++currentMonth,
        nextMonthFirstDay = new Date(date.getFullYear(), nextMonth, 1),
        oneDay = 1000 * 60 * 60 * 24,
        lastDay = new Date(nextMonthFirstDay - oneDay),
        tempLastDay = formatDate(lastDay),
        endLastDay = tempLastDay.substring(tempLastDay.lastIndexOf('-') + 1, tempLastDay.length)
    return endLastDay;
}

export function getThisMonth() {
    return new Date().getMonth() + 1;
}

export function getSubDate(val){
    return val ? val.substring(0, val.lastIndexOf(' ')) : ''
}